import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Colors from "../constants/Colors";
import { unitWidth } from '../constants/AdapterUtil';
import Picker from 'react-native-picker';
import { area } from '../constants/area';

class AreaPicker {

    static myPicker;

    static _createAreaData() {
        let data = [];
        let len = area.length;
        for (let i = 0; i < len; i++) {
            let city = [];
            for (let j = 0, cityLen = area[i]['city'].length; j < cityLen; j++) {
                let _city = {};
                _city[area[i]['city'][j]['name']] = area[i]['city'][j]['area'];
                city.push(_city);
            }

            let _data = {};
            _data[area[i]['name']] = city;
            data.push(_data);
        }
        return data;
    }

    //多了一个全国选项
    static _createAreaDataAll() {
        let data = [];
        data.push({
            '全国': [{
                '全国': ['全国']
            }]
        })
        let len = area.length;
        for (let i = 0; i < len; i++) {
            let city = [];
            for (let j = 0, cityLen = area[i]['city'].length; j < cityLen; j++) {
                let _city = {};
                _city[area[i]['city'][j]['name']] = area[i]['city'][j]['area'];
                city.push(_city);
            }

            let _data = {};
            _data[area[i]['name']] = city;
            data.push(_data);
        }
        return data;
    }

    //组件初始化
    static pickerInitcallback(callback, isAll) {
        Picker.init({
            pickerData: isAll ? this._createAreaDataAll() : this._createAreaData(),
            pickerCancelBtnText: '取消',
            pickerConfirmBtnText: '确定',
            // selectedValue: [this.state.CastellanInfo.province.substring(0, 2), this.state.CastellanInfo.city.substring(0, 2), this.state.CastellanInfo.district],
            pickerTitleText: '',
            onPickerConfirm: data => {
                console.log(data);
                callback.success(data[0], data[1], data[2])
            },
            onPickerCancel: data => {
                console.log(data);
            },
            onPickerSelect: data => {
                console.log(data);
            }
        });

        AreaPicker.myPicker = Picker;
        AreaPicker.myPicker.show();
    }
}

export default AreaPicker;